package model;

import java.io.*;

import javax.sound.midi.*;
/**
 * A sequence listener that listens to change in a sequence.
 * Used to play MIDI files.
 * 
 * @author Stephan Veenstra, Marc Bruins, Michel Jansma
 * @version 1.0
 */

public class PlaySound {
	private String fileSrc;
	private Sequencer sequencer;
	
	
	//Setting the MIDI file path
	public PlaySound(String fileSrc) {
		this.fileSrc = fileSrc;
	}
	
	//initialize sequencelistener
	//Loop continuously
	public void startSound(boolean loop) {
		try{
		sequencer = MidiSystem.getSequencer();

		File midiFile = new File(fileSrc);

		sequencer.setSequence(MidiSystem.getSequence(midiFile));

		if(loop) {
			sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);			
		}
		
		sequencer.open();
		sequencer.start();
		} catch(Exception e){
			System.out.println(e);
		}
	}
	
	//Stop the sound
	public void stopSound() {
		sequencer.stop();
	}
}